import { ReloadOutlined } from '@mao-fu/sun-ui-icon';
import React from 'react';
import { buttonClass } from './style';
import type { ButtonProps } from './types';

const Button: React.FC<ButtonProps> = (props) => {
  const {
    children,
    type: typeProp,
    theme = 'light',
    size = 'default',
    className,
    style,
    onClick,
    loading = false,
    disabled = false,
    block = false,
    htmlType = 'button',
    prefixIcon,
    suffixIcon,
    ...rest
  } = props;

  const classes = buttonClass(
    '_',
    className,
    typeProp || 'primary',
    theme,
    size,
    { loading, disabled, block },
  );

  const handleBtnClick: React.DOMAttributes<HTMLButtonElement>['onClick'] = (
    e,
  ) => {
    if (loading) return;
    if (disabled) return;
    onClick?.(e);
  };

  return (
    <button
      style={style}
      className={classes}
      onClick={handleBtnClick}
      type={htmlType}
      {...rest}
    >
      {loading && <ReloadOutlined spin={loading} />}
      {/* 不在加载状态时，才显示前缀图标 */}
      {!loading && prefixIcon}
      {children}
      {suffixIcon}
    </button>
  );
};

export default Button;
